package com.biostime.qdingding.modules.qd.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.Promotemplate;
import com.biostime.qdingding.modules.qd.service.PromotemplateService;

/**
 * 促销模版Controller
 * @author king
 * @version 2015-11-12
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/promotemplate")
public class PromotemplateController extends BaseController {

	@Autowired
	private PromotemplateService promotemplateService;
	
	@ModelAttribute
	public Promotemplate get(@RequestParam(required=false) String id) {
		Promotemplate entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = promotemplateService.get(id);
		}
		if (entity == null){
			entity = new Promotemplate();
		}
		return entity;
	}
	
	@RequiresPermissions("qd:promotemplate:view")
	@RequestMapping(value = {"list", ""})
	public String list(Promotemplate promotemplate, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Promotemplate> page = promotemplateService.findPage(new Page<Promotemplate>(request, response), promotemplate); 
		model.addAttribute("page", page);
		return "modules/qd/promotemplateList";
	}

	@RequiresPermissions("qd:promotemplate:view")
	@RequestMapping(value = "form")
	public String form(Promotemplate promotemplate, Model model) {
		model.addAttribute("promotemplate", promotemplate);
		return "modules/qd/promotemplateForm";
	}

	@RequiresPermissions(value={"qd:promotemplate:edit","qd:promotemplate:add"}, logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Promotemplate promotemplate, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, promotemplate)){
			return form(promotemplate, model);
		}
		promotemplateService.save(promotemplate);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/qd/promotemplate/?repage";
	}
	
	@RequiresPermissions("qd:promotemplate:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Promotemplate promotemplate, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				promotemplateService.delete(new Promotemplate(id));
			}
		}else{
			promotemplateService.delete(promotemplate);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/qd/promotemplate/?repage";
	}

}